/**
 * 检查订单状态脚本
 */

const { query } = require('../config/db');

async function checkOrders() {
  try {
    console.log('🔍 检查订单状态...\n');

    // 查询最近的订单
    const recentOrdersQuery = `
      SELECT 
        id, order_number, status, license_plate,
        planned_start_time, planned_end_time, 
        actual_start_time, actual_end_time,
        created_at, updated_at
      FROM orders 
      ORDER BY created_at DESC 
      LIMIT 10
    `;

    const orders = await query(recentOrdersQuery);
    
    console.log('📋 最近10个订单:');
    orders.forEach((order, index) => {
      console.log(`\n${index + 1}. 订单号: ${order.order_number} (${order.license_plate})`);
      console.log(`   状态: ${order.status}`);
      console.log(`   预计入场: ${order.planned_start_time}`);
      console.log(`   预计离场: ${order.planned_end_time}`);
      if (order.actual_start_time) {
        console.log(`   实际入场: ${order.actual_start_time}`);
      }
      if (order.actual_end_time) {
        console.log(`   实际离场: ${order.actual_end_time}`);
      }
      console.log(`   创建时间: ${order.created_at}`);
      console.log(`   更新时间: ${order.updated_at}`);
    });

    // 查询进行中的订单
    console.log('\n🚗 进行中的订单:');
    const inProgressQuery = `
      SELECT 
        id, order_number, status, license_plate,
        planned_start_time, planned_end_time, 
        actual_start_time, actual_end_time
      FROM orders 
      WHERE status = 'in_progress'
      ORDER BY planned_end_time ASC
    `;

    const inProgressOrders = await query(inProgressQuery);
    
    if (inProgressOrders.length > 0) {
      inProgressOrders.forEach((order, index) => {
        console.log(`\n${index + 1}. ${order.order_number} (${order.license_plate})`);
        console.log(`   预计离场: ${order.planned_end_time}`);
        if (order.actual_start_time) {
          console.log(`   实际入场: ${order.actual_start_time}`);
        }
      });
    } else {
      console.log('没有进行中的订单');
    }

    // 查询特定车牌的订单
    console.log('\n🔍 查询京A2345的订单:');
    const specificOrderQuery = `
      SELECT 
        id, order_number, status, license_plate,
        planned_start_time, planned_end_time, 
        actual_start_time, actual_end_time,
        created_at, updated_at
      FROM orders 
      WHERE license_plate = '京A2345'
      ORDER BY created_at DESC 
      LIMIT 5
    `;

    const specificOrders = await query(specificOrderQuery);
    
    if (specificOrders.length > 0) {
      specificOrders.forEach((order, index) => {
        console.log(`\n${index + 1}. ${order.order_number}`);
        console.log(`   状态: ${order.status}`);
        console.log(`   预计入场: ${order.planned_start_time}`);
        console.log(`   预计离场: ${order.planned_end_time}`);
        if (order.actual_start_time) {
          console.log(`   实际入场: ${order.actual_start_time}`);
        }
        if (order.actual_end_time) {
          console.log(`   实际离场: ${order.actual_end_time}`);
        }
        console.log(`   创建时间: ${order.created_at}`);
        console.log(`   更新时间: ${order.updated_at}`);
      });
    } else {
      console.log('没有找到京A2345的订单');
    }

    console.log('\n✅ 订单检查完成');

  } catch (error) {
    console.error('❌ 订单检查失败:', error);
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  checkOrders().then(() => {
    process.exit(0);
  }).catch(error => {
    console.error('脚本执行失败:', error);
    process.exit(1);
  });
}

module.exports = { checkOrders };
